/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.event;

import com.google.gwt.event.shared.GwtEvent;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class OrderChangedEvent<I> extends GwtEvent<OrderChangedHandler<I>> {

    /**
     * Handler type.
     */
    private static Type<OrderChangedHandler< ? >> TYPE;

    /**
     * Fires a Over event on all registered handlers in the handler manager.If
     * no such handlers exist, this method will do nothing.
     * 
     * @param <I>
     *            the selected item type
     * @param source
     *            the source of the handlers
     * @param oldELement
     *            the selected item
     */
    public static <I> void fire(HasOrderChangedHandlers<I> source,
            I oldElement, I newElement, boolean changedMask[]) {
        if (TYPE != null) {
            OrderChangedEvent<I> event = new OrderChangedEvent<I>(oldElement,
                    newElement, changedMask);
            source.fireEvent(event);
        }
    }

    /**
     * Gets the type associated with this event.
     * 
     * @return returns the handler type
     */
    public static Type<OrderChangedHandler< ? >> getType() {
        if (TYPE == null) {
            TYPE = new Type<OrderChangedHandler< ? >>();
        }
        return TYPE;
    }

    private final I oldElement;

    private final I newElement;

    private boolean[] changedMask;

    /**
     * Creates a new Over event.
     * 
     * @param oldElement
     *            selected item
     */
    protected OrderChangedEvent(I oldElement, I newElement,
            boolean changedMask[]) {
        this.oldElement = oldElement;
        this.newElement = newElement;

        this.changedMask = changedMask;
    }

    // The instance knows its BeforeOverHandler is of type I, but the TYPE
    // field itself does not, so we have to do an unsafe cast here.
    @SuppressWarnings("unchecked")
    @Override
    public final Type<OrderChangedHandler<I>> getAssociatedType() {
        return (Type) TYPE;
    }

    /**
     * Gets the selected item.
     * 
     * @return the selected item
     */
    public I getOldOrderState() {
        return oldElement;
    }

    public I getNewOrderState() {
        return newElement;
    }

    public boolean[] getChangedMask() {
        return changedMask;
    }

    @Override
    protected void dispatch(OrderChangedHandler<I> handler) {
        handler.onOrderChanged(this);
    }
}
